package com.smile.service.impl;

import com.alibaba.nacos.common.utils.MD5Utils;
import com.smile.model.dto.request.SpeakerAudioPlayStartRequestDTO;
import com.smile.model.dto.request.SpeakerPlayModeSetRequestDTO;
import com.smile.model.dto.request.SpeakerPlayVolumeSetRequestDTO;
import com.smile.model.dto.request.SpeakerTtsPlayStartRequestDTO;
import com.smile.service.SpeakerService;
import com.smile.service.task.SpeakServicesExecutor;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.stereotype.Service;

@Service
public class SpeakerServiceImpl implements SpeakerService {


    @Override
    public void speakerPlayVolumeSet(SpeakerPlayVolumeSetRequestDTO speakerPlayVolumeSetRequestDTO) {
        try {
            SpeakServicesExecutor.create()
                    .speakerPlayVolumeSet(speakerPlayVolumeSetRequestDTO.getPlayVolume())
                    .gateway(speakerPlayVolumeSetRequestDTO.getGateway())
                    .execute();
        } catch (MqttException me) {
            me.printStackTrace();
        }
    }

    @Override
    public void speakerPlayModeSet(SpeakerPlayModeSetRequestDTO speakerPlayModeSetRequestDTO) {
        try {
            SpeakServicesExecutor.create()
                    .speakerPlayModeSet(speakerPlayModeSetRequestDTO.getPlayMode())
                    .gateway(speakerPlayModeSetRequestDTO.getGateway())
                    .execute();
        } catch (MqttException me) {
            me.printStackTrace();
        }
    }

    @Override
    public void speakerTtsPlayStart(SpeakerTtsPlayStartRequestDTO speakerTtsPlayStartRequestDTO) {
        try {
            String md5 = "";
            try {
                md5 = MD5Utils.md5Hex(speakerTtsPlayStartRequestDTO.getText().getBytes());
            }catch (Exception e) {

            }
            speakerTtsPlayStartRequestDTO.setMd5(md5);
            SpeakServicesExecutor.create().
                    speakerTtsPlayStart(speakerTtsPlayStartRequestDTO.getName(),
                            speakerTtsPlayStartRequestDTO.getText(),
                            speakerTtsPlayStartRequestDTO.getMd5())
                    .gateway(speakerTtsPlayStartRequestDTO.getGateway())
                    .execute();
        } catch (MqttException me) {
        }
    }

    @Override
    public void speakerAudioPlayStartRequest(SpeakerAudioPlayStartRequestDTO speakerAudioPlayStartRequestDTO) {
        try {
            SpeakServicesExecutor.create()
                    // name url md5
                    .speakerAudioPlayStart(speakerAudioPlayStartRequestDTO.getName(), speakerAudioPlayStartRequestDTO.getUrl(),
                            speakerAudioPlayStartRequestDTO.getMd5())
                    .gateway(speakerAudioPlayStartRequestDTO.getGateway())
                    .execute();
        } catch (MqttException me) {
        }
    }

}
